package erwiki.ui.app.admin.views;

import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import erwiki.admin.internal.ProfileOperations;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;

public class UserProfile {

	public static final String ID = "org.elwiki.e4.ui.app.admin.part.userprofie"; //$NON-NLS-1$

	private Text textLoginId;
	private Text textPassword;
	private Text textLoginName;
	private Text textEmailAddress;

	@Inject
	private MPart part;

	// == CODE ================================================================

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 * @wbp.parser.entryPoint
	 */
	@PostConstruct
	public void createComposite(Composite parent) {
		/*:FVK: -- из примера ... :: обратить внимание на доступ к this.part
		Text txtInput = new Text(parent, SWT.BORDER);
		txtInput.setMessage("Enter text to mark part as dirty (org.elwiki.e4.ui.app.admin)");
		txtInput.addModifyListener(e -> part.setDirty(true));
		txtInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		 */

		parent.setLayout(new GridLayout(1, false));
		{
			Group grpUser = new Group(parent, SWT.NONE);
			GridData gd_grpUser = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
			gd_grpUser.widthHint = 395;
			grpUser.setLayoutData(gd_grpUser);
			grpUser.setText("User");
			grpUser.setLayout(new GridLayout(2, false));
			GridData gd_textLoginId = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
			gd_textLoginId.widthHint = 220;
			{
				Label lblLoginId = new Label(grpUser, SWT.NONE);
				lblLoginId.setText("Login name");
			}
			{
				this.textLoginId = new Text(grpUser, SWT.BORDER);
				this.textLoginId.setLayoutData(gd_textLoginId);
			}
			{
				Label lblPassword1 = new Label(grpUser, SWT.NONE);
				lblPassword1.setText("Password");
			}
			{
				this.textPassword = new Text(grpUser, SWT.BORDER);
				this.textPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			}
			{
				Label lblLoginName = new Label(grpUser, SWT.NONE);
				lblLoginName.setText("Name");
			}
			{
				this.textLoginName = new Text(grpUser, SWT.BORDER);
				this.textLoginName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			}
			{
				Label lblEmailAddress = new Label(grpUser, SWT.NONE);
				lblEmailAddress.setText("E-mail address");
			}
			{
				this.textEmailAddress = new Text(grpUser, SWT.BORDER);
				this.textEmailAddress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			}
			new Label(grpUser, SWT.NONE);
			{
				Button btnCreateNewUser = new Button(grpUser, SWT.NONE);
				btnCreateNewUser.addMouseListener(new MouseAdapter() {
					private static final long serialVersionUID = 1L;

					@Override
					public void mouseUp(MouseEvent e) {
						createNewUserProfile();
					}
				});
				btnCreateNewUser.setText("Create new user profile");
			}
		}

		/*:FVK: old code.
		createActions();
		initializeToolBar();
		initializeMenu();*/
	}

	protected void createNewUserProfile() {
		ProfileOperations.createUserProfile(//@formatter:off
				this.textLoginId.getText(),
				this.textLoginName.getText(),
				this.textPassword.getText(),
				this.textPassword.getText(),
				this.textEmailAddress.getText());//@formatter:on
	}
	
	@Persist
	public void save() {
		/*:FVK: -- из примера ...
		  part.setDirty(false);
		 */
	}

}